package com.zhou.bjgl.bjgl.business_web.controller;


import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.mysql.cj.util.StringUtils;
import com.zhou.bjgl.bjgl.business_web.service.CommonService;
import com.zhou.bjgl.bjgl.exception.BwException;
import com.zhou.bjgl.bjgl.exception.BwExceptionEnum;
import com.zhou.bjgl.bjgl.myAnnotations.UserLoginToken;
import com.zhou.bjgl.bjgl.req.BaseModel;
import com.zhou.bjgl.bjgl.req.b_student.BStudentQueryReq;
import com.zhou.bjgl.bjgl.req.user.UserReq;
import com.zhou.bjgl.bjgl.resp.excel_import.BStudentExcel;
import com.zhou.bjgl.bjgl.resp.student.BStudentQueryResp;
import com.zhou.bjgl.bjgl.resp.PageResp;
import com.zhou.bjgl.bjgl.resp.common.ApiRestResponse;
import com.zhou.bjgl.bjgl.business_web.service.BStudentService;
import com.zhou.bjgl.bjgl.resp.user.BUserForInfoResp;
import com.zhou.bjgl.bjgl.util.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * <p>
 *  学生管理模块
 * </p>
 *
 * @author jz
 * @since 2021-07-04
 */
@RestController
@RequestMapping("/bjgl/student")
@Api("学生管理")
public class BStudentController extends BaseModel {
    @Resource
    private BStudentService bStudentService;

    @Resource
    private CommonService commonService;

    @ApiOperation("添加学生")
    @PostMapping("/add")
    @UserLoginToken
    public ApiRestResponse add(BStudentQueryReq req){
        //查询是否有相同用户名
            UserReq userReq = new UserReq();
            userReq.setUsername(req.getUsername());
            BUserForInfoResp userInfo = commonService.getUserInfoForEdit(userReq);
            if (userInfo != null) {
                if ( "edit".equals(req.getTypeForEdit()) && !ObjectUtils.nullSafeEquals(req.getId(), userInfo.getId())) {
                    throw new BwException(BwExceptionEnum.NAME_EXISTED);
                }
                if ("add".equals(req.getTypeForEdit())){
                    throw new BwException(BwExceptionEnum.NAME_EXISTED);
                }

            }
        bStudentService.add(req);
        return ApiRestResponse.success();
    }

    @ApiOperation("查询学生信息列表")
    @GetMapping("/query")
    @ResponseBody
    @UserLoginToken
    public ApiRestResponse query(BStudentQueryReq req){
        req.setClassId(threadLocal.get().getClassId());
        PageResp<BStudentQueryResp> queryPage = bStudentService.queryStu(req);
        return ApiRestResponse.success(queryPage);
    }

    @ApiOperation("删除学生")
    @PostMapping("/delete")
    @UserLoginToken
    public ApiRestResponse delete(@RequestBody BStudentQueryReq req) {
        bStudentService.deleteStu(req);
        return ApiRestResponse.success();
    }

    @ApiOperation("导入学生信息")
    @PostMapping("/import")
    public ApiRestResponse importInfo() {

        return ApiRestResponse.success();
    }

    @ApiOperation("获取导出模板")
    @PostMapping("getImportTemplate")
    public void getImportTemplate(HttpServletResponse response) {
        try {
            ExcelUtils.exportExcel(new ArrayList<BStudentExcel>(), BStudentExcel.class,"学生信息",new ExportParams(),response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

